package cn.ht.jywrwater.module.ht.controller.admin.customermeterdevice;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.customermeterdevice.vo.*;
import cn.ht.jywrwater.module.ht.convert.customermeterdevice.CustomerMeterDeviceConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.customermeterdevice.CustomerMeterDeviceDO;
import cn.ht.jywrwater.module.ht.service.customermeterdevice.CustomerMeterDeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 小口径流量")
@RestController
@RequestMapping("/ht/customer-meter-device")
@Validated
public class CustomerMeterDeviceController {

    @Resource
    private CustomerMeterDeviceService customerMeterDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建小口径流量")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter-device:create')")
    public CommonResult<Long> createCustomerMeterDevice(@Valid @RequestBody CustomerMeterDeviceCreateReqVO createReqVO) {
        return success(customerMeterDeviceService.createCustomerMeterDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小口径流量")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter-device:update')")
    public CommonResult<Boolean> updateCustomerMeterDevice(@Valid @RequestBody CustomerMeterDeviceUpdateReqVO updateReqVO) {
        customerMeterDeviceService.updateCustomerMeterDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小口径流量")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:customer-meter-device:delete')")
    public CommonResult<Boolean> deleteCustomerMeterDevice(@RequestParam("id") Long id) {
        customerMeterDeviceService.deleteCustomerMeterDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小口径流量")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter-device:query')")
    public CommonResult<CustomerMeterDeviceRespVO> getCustomerMeterDevice(@RequestParam("id") Long id) {
        CustomerMeterDeviceDO customerMeterDevice = customerMeterDeviceService.getCustomerMeterDevice(id);
        return success(CustomerMeterDeviceConvert.INSTANCE.convert(customerMeterDevice));
    }

    @GetMapping("/list")
    @Operation(summary = "获得小口径流量列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter-device:query')")
    public CommonResult<List<CustomerMeterDeviceRespVO>> getCustomerMeterDeviceList(@RequestParam("ids") Collection<Long> ids) {
        List<CustomerMeterDeviceDO> list = customerMeterDeviceService.getCustomerMeterDeviceList(ids);
        return success(CustomerMeterDeviceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小口径流量分页")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter-device:query')")
    public CommonResult<PageResult<CustomerMeterDeviceRespVO>> getCustomerMeterDevicePage(@Valid CustomerMeterDevicePageReqVO pageVO) {
        PageResult<CustomerMeterDeviceDO> pageResult = customerMeterDeviceService.getCustomerMeterDevicePage(pageVO);
        return success(CustomerMeterDeviceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出小口径流量 Excel")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter-device:export')")
    @OperateLog(type = EXPORT)
    public void exportCustomerMeterDeviceExcel(@Valid CustomerMeterDeviceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CustomerMeterDeviceDO> list = customerMeterDeviceService.getCustomerMeterDeviceList(exportReqVO);
        // 导出 Excel
        List<CustomerMeterDeviceExcelVO> datas = CustomerMeterDeviceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "小口径流量.xls", "数据", CustomerMeterDeviceExcelVO.class, datas);
    }

}
